/*
 * Author :pandiam
 */
$.ajaxSetup({ cache: false });

Array.prototype.remove = function (from, to)
{
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

function autocomplete()
{
    $("input#moderator").autocomplete(
    {
        source: data2
    });
    $("input#user").autocomplete(
    {
        source: data2
    });
}

function toggle(argument)
{
    if (argument == 2) document.getElementById("hide_this").style.display = 'none';
    else document.getElementById("hide_this").style.display = '';
}

function toggleSelect()
{
    if (document.getElementById('best').selectedIndex == 2) document.getElementById('close_to').style.visibility = 'visible';
    else document.getElementById('close_to').style.visibility = 'hidden';
}

/*
 *  Ayto to kommati afora ta attributes kai thn diaxeirish tous sto
 *  editCategory.php
 */
var obj;
if (obj == null) obj = new Array();

function convert(argument)
{
    var lame = argument.split(",");
    for (var j = 0; j < lame.length; j++)
    {
        obj.push(lame[j]);
    }
}

function deleteAttr(argument)
{
    obj.remove(argument);
    changeContent("delete");
}

function keepAttribute()
{
    var name = document.getElementById('attrname');
    var button1 = null;
    var best_val = null;
    var close_to = null;
    var checkString1 = "";
    var checkString2 = "";
    var checkString3 = "";
    if (document.editCategory.type[0].checked) button1 = document.editCategory.type[0].value;
    else if (document.editCategory.type[1].checked) button1 = document.editCategory.type[1].value;
    else button1 = document.editCategory.type[2].value;
    //na rwthsw gia to measurable kai to unmeasurable
    if (button1 == "measurable")
    {
        best_val = document.getElementById('best');
        if (best_val.value == "close_to")
        {
            close_to = document.getElementById('close_to');
            //edw mporei na xreiastoume elegxo gia to close_to
            checkString1 = close_to.value;
        }
        checkString2 = best_val.value + "~" + checkString1;
    }
    if (name.value.length == 0) alert("Attribute name is not completed");
    else if (checkDoubleData(name.value) == false)
        alert("You have already entered an attribute with the given name");
    else if (checkNameValidation(name.value) == false)
        alert("The given name is not in the right form. The name must not contain special characters"); //TODO: Maybe permit them?
    else
    {
        checkString3 = name.value + "~" + button1 + "~" + checkString2;
        obj.push(checkString3);
        changeContent(checkString3);
        document.getElementById('attrname').value = "";
        document.editCategory.type[0].checked = true;
        toggle(1);
        document.getElementById('best').value = "min";
        document.getElementById('best').selectedIndex = 0;
        document.getElementById('close_to').value = "";
        document.getElementById('close_to').style.visibility = 'hidden';
    }
}

//na koitaxw na ftiaxw ligo tous elegxous kai thn ektypwsh!!
function checkNameValidation(name)
{
    var regex = /^[A-Za-z0-9\(][0-9A-Za-z\(\)_\s-]+$/;
    if (!regex.test(name)) return false;
    return true;
}

function checkDoubleData(name)
{
    for (var i = 0; i < obj.length; i++)
    {
        var split = obj[i].split("~");
        if (split[0].toLowerCase() == name.toLowerCase()) return false;
    }
    return true;
}

function changeContent(argument)
{
    var deleteLik;
    if (argument != "delete")
    {
        deleteLik = "<a href=\"javascript:deleteAttr( " + (obj.length - 1) + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
        $("#attrCat").html($("#attrCat").html() + argument.replace(/~/gi, " ") + " " + deleteLik + " " + "<br/>");
    }
    else
    {
        var htmlCode = "";
        for (var i = 0; i < obj.length; i++)
        {
            deleteLik = "<a href=\"javascript:deleteAttr( " + i + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
            htmlCode = htmlCode + obj[i].replace(/~/gi, " ") + " " + deleteLik + " " + "<br/>";
        }
        $("#attrCat").html(htmlCode);
    }
}

/*
 * Ayto edw to kommati afora tous users kai thn diaxeirish tous
 * sto editCategory
 */
var users;
if (users == null) users = new Array();

function convertUsers(argument)
{
    var lame = argument.split(",");
    for (var j = 0; j < lame.length; j++)
    {
        users.push(lame[j]);
    }
}

function deleteUser(argument)
{
    data2.push(users[argument]);
    users.remove(argument);
    changeContentUser("delete");
    autocomplete();
}

function checkUserList(name)
{
    for (var i = 0; i < data2.length; i++)
    {
        if (data2[i] == name) return i;
    }
    return -1;
}

function keepUser()
{
    var name = document.getElementById('user').value;
    var result = checkUserList(name);
    if (result != -1)
    {
        users.push(name);
        data2.remove(result);
        changeContentUser(name);
        document.getElementById('user').value = "";
        autocomplete();
    }
    else
    {
        alert("The user does not exist or already participates in this category.")
    }
}

function changeContentUser(argument)
{
    var deleteLik;
    if (argument != "delete")
    {
        deleteLik = "<a href=\"javascript:deleteUser( " + (users.length - 1) + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
        $("#attrCat3").html($("#attrCat3").html() + argument + " " + deleteLik + " " + "<br/>");
    }
    else
    {
        var htmlCode = "";
        for (var i = 0; i < users.length; i++)
        {
            deleteLik = "<a href=\"javascript:deleteUser( " + i + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
            htmlCode = htmlCode + users[i] + " " + deleteLik + " " + "<br/>";
        }
        $("#attrCat3").html(htmlCode);
    }
}

/*
 * Ayto to kommati afora tous moderators pou eisagontai sthn kathgoria apo ton user
 */
var mods;
if (mods == null) mods = new Array();

function convertMods(argument)
{
    var lame = argument.split(",");
    for (var j = 0; j < lame.length; j++)
    {
        mods.push(lame[j]);
    }
}

function deleteMod(argument)
{
    data2.push(mods[argument]);
    mods.remove(argument);
    changeContentMods("delete");
    autocomplete();
}

function keepModerator()
{
    var name = document.getElementById('moderator').value;
    var result = checkUserList(name);
    if (result != -1)
    {
        mods.push(name);
        data2.remove(result);
        changeContentMods(name);
        document.getElementById('moderator').value = "";
        autocomplete();
    }
    else
    {
        alert("The user does not exist or already participates in this category.")
    }
}

function changeContentMods(argument)
{
    var deleteLik;
    if (argument != "delete")
    {
        deleteLik = "<a href=\"javascript:deleteMod( " + (mods.length - 1) + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
        $("#attrCat2").html($("#attrCat2").html() + argument + " " + deleteLik + " " + "<br/>");
    }
    else
    {
        var htmlCode = "";
        for (var i = 0; i < mods.length; i++)
        {
            deleteLik = "<a href=\"javascript:deleteMod( " + i + ")\" ><img style=\"margin-top:10px;\" align=\"center\" height=\"20px\" src=\"images/delete_button.png\" alt=\"delete\"/></a>";
            htmlCode = htmlCode + mods[i] + " " + deleteLik + " " + "<br/>";
        }
        $("#attrCat2").html(htmlCode);
    }
}

//elelgxoume gia ola ta dedomena pou exei eisagei o xrhsths kai kanoume redirect
//sthn katallhlh selida an ola einai kala
function passData()
{
    if (document.getElementById('catname').value.length == 0)
        alert("Category name is not completed");
    else if (obj.length == 0)
        alert("You must insert at least 1 attribute to this category");
    else
    {
        document.editCategory.attrs.value = obj;
        document.editCategory.mods.value = mods;
        document.editCategory.users.value = users;
        document.editCategory.irrelev.value = data2;
        document.editCategory.submit();
    }
}